package de.openlaboratory.xmleditor.servlet.listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

@WebListener
public class InitCacheListener implements ServletContextListener
{
	ServletContext servletContext;
	
	@Override
	public void contextDestroyed(ServletContextEvent sce)
	{
		CacheManager.getInstance().clearAll();
		CacheManager.getInstance().shutdown();

	}

	@Override
	public void contextInitialized(ServletContextEvent sce)
	{
		servletContext = sce.getServletContext();
		
		CacheManager.getInstance().addCache("xyz");
		
//        CacheManager singletonManager = CacheManager.create();
//        Cache memoryOnlyCache = new Cache("dbCache", 100, false, true, 86400,86400);
//        singletonManager.addCache(memoryOnlyCache);
//        cache = singletonManager.getCache("dbCache");       
//        ctx.setAttribute("dbCache", cache );
        
		Cache xyz = CacheManager.getInstance().getCache("xyz");
		xyz.put(new Element("myKey", "1324987"));
	}

}
